package com.pureut.scheduling.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.scheduling.domain.SchedulingHours;
import com.pureut.scheduling.domain.SchedulingHoursLine;
import com.pureut.scheduling.domain.dto.SchedulingHoursDto;
import com.pureut.scheduling.domain.dto.SchedulingHoursLineDto;
import com.pureut.scheduling.domain.dto.SchedulingHoursLineSDto;
import com.pureut.scheduling.domain.vo.SchedulingHoursVo;
import com.pureut.scheduling.mapper.SchedulingHoursLineMapper;
import com.pureut.scheduling.mapper.SchedulingHoursMapper;
import com.pureut.scheduling.service.ISchedulingHoursService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 标准工时实现类
 * @Param SchedulingHours
 * @Date 22-12-23
 * @Author hl
 **/
@Service
public class SchedulingHoursServiceImpl implements ISchedulingHoursService {

    @Resource
    private SchedulingHoursMapper schedulingHoursMapper;

    @Resource
    private SchedulingHoursLineMapper schedulingHoursLineMapper;

    /**
     * 查询标准工时
     *
     * @param schedulingHoursVo 标准工时接受类
     * @return list schedulingHoursVo
     */
    @Override
    @DataScope(deptAlias = "scheduling_hours")
    public List<SchedulingHoursDto> selectHoursList(SchedulingHoursVo schedulingHoursVo) {
        List<SchedulingHoursDto> list = schedulingHoursMapper.selectHoursList(schedulingHoursVo);
        List<SysDictData> dictList= DictUtils.getDictCache("scheduling_hours_unit");
        assert dictList != null;
        Map<String, String> actualMap = dictList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        for (SchedulingHoursDto dto:
        list) {
            dto.setHoursUnitDict(actualMap.get(String.valueOf(dto.getHoursUnit())));
            if(dto.getHoursMaterialType()==3){
                dto.setMaterialTypeDict("半成品");
            }else{
                dto.setMaterialTypeDict("成品");
            }
        }
        return list;
    }

    /**
     * 新增标准工时
     *
     * @param schedulingHoursVo 标准工时接受类
     * @return 成功 1 失败 0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSchedulingHours(SchedulingHours schedulingHours) throws Exception{
        schedulingHours.setDeptId(SecurityUtils.getDeptId());
        int count = schedulingHoursMapper.countSchedulingHours(schedulingHours.getHoursMaterialCode());
        if(count>0){
            throw new SQLException("该物料编码已存在标准工时,请勿重复添加");
        }
        int os = schedulingHoursMapper.insertSchedulingHours(schedulingHours);
        if(os>0){
            long hoId = schedulingHours.getHoursId();
            os= addSchedulingHoursLine(schedulingHours.getHoursLineCode(),hoId);
        }else{
            throw new SQLException("新增失败");
        }
        return os;
    }

    /**
     * 修改标准工时
     *
     * @param schedulingHoursVo 标准工时接受类
     * @return 成功 1 失败 0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSchedulingHoursEdit(SchedulingHours schedulingHours) throws Exception{
        int upl = schedulingHoursMapper.updateSchedulingHoursEdit(schedulingHours);
        if(upl>0){
            int del=schedulingHoursLineMapper.deleteHoursLineByOnId(schedulingHours.getHoursId());
            if(del==0){
                throw new SQLException("修改失败");
            }
            upl= addSchedulingHoursLine(schedulingHours.getHoursLineCode(),schedulingHours.getHoursId());
        }else{
            throw new SQLException("修改失败");
        }
        return upl;
    }

    /**
     * 删除标准工时
     *
     * @param hoursId 标准工时Id
     * @return 成功 1 失败 0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteHoursById(String hoursId) throws Exception{
        String[] hoursIdS = hoursId.split(",");
        int del = schedulingHoursMapper.deleteHoursById(hoursIdS);
        if(del>0){
            del=schedulingHoursLineMapper.deleteHoursLineById(hoursIdS);
            if(del==0){
                throw new SQLException("删除失败");
            }
        }else{
            throw new SQLException("删除失败");
        }
        return del;
    }

    /**
     * 查询单个标准工时
     *
     * @param hoursId 标准工时Id
     * @return SchedulingHoursDto 标准工时返回类
     */
    @Override
    public SchedulingHoursDto selectHoursById(long hoursId) {
        SchedulingHoursVo schedulingHoursVo = new SchedulingHoursVo();
        schedulingHoursVo.setHoursId(hoursId);
        List<SchedulingHoursDto> list = schedulingHoursMapper.selectHoursList(schedulingHoursVo);
        SchedulingHoursDto hours = list.get(0);
        //代表半成品
        if(hours.getHoursMaterialType()==3){
            hours.setMaterialTypeDict("半成品");
        }else{
            hours.setMaterialTypeDict("成品");
        }
        List<SchedulingHoursLine> liList = schedulingHoursLineMapper.selectHoursLineList(hoursId);
        List<SchedulingHoursLineSDto> listD = new ArrayList<>();
        for (SchedulingHoursLine line:
        liList) {
            SchedulingHoursLineSDto dto = new SchedulingHoursLineSDto();
            String echo = line.getEchoId();
            String[] echoS = echo.split(",");
            List<Long> lo = new ArrayList<>();
            for (String kl:
            echoS) {
                lo.add(Long.parseLong(kl));
            }
            dto.setEchoId(lo);
            dto.setLineChangeDuration(line.getLineChangeDuration());
            dto.setLineWireDuration(line.getLineWireDuration());
            dto.setLineYieldRatio(line.getLineYieldRatio());
            listD.add(dto);
        }
        SchedulingHoursLineDto hoursLineDto = new SchedulingHoursLineDto();
        hoursLineDto.setLineCode(listD);
        hours.setHoursLineCode(hoursLineDto);
        return hours;
    }

    /**
     * 根据线别id查询是否存在
     *
     * @param hoursLineId 线别id
     * @return SchedulingHoursDto 标准工时返回类
     */
    @Override
    public int selectHoursLineById(long hoursLineId) {
        return schedulingHoursMapper.selectHoursLineById(hoursLineId);
    }

    @Override
    public SchedulingHoursDto selectHoursOnById(long lineId, String materialCode) {
        return schedulingHoursMapper.selectHoursOnById(lineId,materialCode);
    }

    /**
     * 标准工时公共方法
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public int addSchedulingHoursLine(JSONObject json,long idS) throws Exception{
        JSONArray arr = json.getJSONArray("lineCode");
        List<SchedulingHoursLine> list= JSONArray.parseArray(arr.toJSONString(), SchedulingHoursLine.class);
        for (SchedulingHoursLine line:
                list) {
            line.setLineHoursId(idS);
            String shopId = line.getEchoId();
            String[] shopIdS = shopId.split(",");
            int len = shopIdS.length;
            line.setLineShopId(Long.parseLong(shopIdS[len-1]));
        }
        int os=schedulingHoursLineMapper.insertSchedulingHoursLine(list);
        if(os==0){
            throw new SQLException("抛出异常");
        }
        return os;
    }
}
